package spring_learn.a11;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


//jdk提供的代理只能基于interface
//jdk 动态代理要求目标**必须**实现接口，生成的代理类实现相同接口，因此代理与目标之间是平级兄弟关系
public class JdkProxyDemo {

    interface hello{
        void sayHello();
    }

    static class chHello implements hello{

        @Override
        public void sayHello() {
            System.out.println("你好");
        }
    }

    public static void main(String[] args) {
        chHello chHello=new chHello();
        //需要的三个参数 classloader interfaces InvocationHandler
        hello o = (hello)Proxy.newProxyInstance(JdkProxyDemo.class.getClassLoader(), new Class[]{hello.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("before");
                Object result = method.invoke(chHello, args);
                System.out.println("after");
                return result;
            }
        });
        o.sayHello();
    }
}
